package com.zhj.leetcode;

import java.util.Arrays;

/**
 * 力扣 283 将0移动到最后
 * @author zhj
 */
public class Test283 {
    public static void main(String[] args) {
        int[] arr = new int[]{0,1,0,3,12};
        move3(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void move1(int[] arr) {
        int cur = -99;
        for (int j = 0; j < arr.length; j++) {
            for (int i = 0; i < arr.length; i++) {
                if ((cur + 1) == i) {
                    arr[cur] = arr[i];
                    arr[i] = 0;
                }
                if (arr[i] == 0) {
                    cur = i;
                }
            }
        }
    }
    private static void move2(int[] arr) {
        int cur = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 0) {
                arr[cur] = arr[i];
                cur++;
            }
        }
        for (; cur < arr.length; cur++) {
            arr[cur] = 0;
        }
    }

    private static void move3(int[] arr) {
        int cur = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 0) {
                int temp = arr[i];
                arr[i] = arr[cur];
                arr[cur++] = temp;
            }
        }
    }
}
